package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueService;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;

    @Autowired
    private ITbClueService iTbClueService;


    /**
     * 添加跟进记录
     * @param clueTrackRecordVo
     * @return
     */
    @Override
    public int add(ClueTrackRecordVo clueTrackRecordVo) {
        //获取当前时间
        Date nowDate = DateUtils.getNowDate();
        //获取跟进人的姓名
        String username = SecurityUtils.getUsername();
        //修改线索表
        TbClue tbClue = new TbClue();
        BeanUtils.copyProperties(clueTrackRecordVo,tbClue);
        tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        tbClue.setId(clueTrackRecordVo.getClueId());
        iTbClueService.updateTbClue(tbClue);

        //添加线索跟进记录
        TbClueTrackRecord tbClueTrackRecord = new TbClueTrackRecord();
        BeanUtils.copyProperties(clueTrackRecordVo,tbClueTrackRecord);
        tbClueTrackRecord.setCreateTime(nowDate);
        tbClueTrackRecord.setCreateBy(username);

        int row = tbClueTrackRecordMapper.add(tbClueTrackRecord);


        return row;
    }

    /**
     * 根据线索id查询线索跟进记录表
     *
     * @param clueId
     * @return
     */
    @Override
    public List<TbClueTrackRecord> findByClueId(Long clueId) {
        List<TbClueTrackRecord> list = tbClueTrackRecordMapper.findByClueId(clueId);
        return list;
    }
}
